package com.jcfk.eam.service.base;


import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamFaultHandleMeasureDao;
import com.jcfk.eam.domain.dto.base.EamFaultHandleMeasureDTO;
import com.jcfk.eam.domain.po.base.EamFaultHandleMeasure;
import com.jcfk.eam.domain.po.base.EamFaultReason;
import com.jcfk.eam.domain.query.base.EamFaultHandleMeasureQuery;
import com.jcfk.eam.domain.uploadExcel.base.EamFaultHandleMeasureUploadExcel;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 故障处理措施
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-10-19
 */
@Service
public class EamFaultHandleMeasureService extends BaseService<EamFaultHandleMeasureDao, EamFaultHandleMeasure> {

    /**
     * EamFaultHandleMeasure分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamFaultHandleMeasureDTO> queryByPage(DataPage<EamFaultHandleMeasureDTO> page, EamFaultHandleMeasureQuery query) {
        // 分页查询
        query.setFaultHandleMeasure(StringUtils.isEmpty(query.getFaultHandleMeasure()) ? query.getFaultHandleMeasure() : query.getFaultHandleMeasure() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamFaultHandleMeasure是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamFaultHandleMeasureDTO dto) {
        QueryWrapper<EamFaultHandleMeasure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入故障处理措施
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamFaultHandleMeasureDTO dto) {

        // 保存
        EamFaultHandleMeasure item = new EamFaultHandleMeasure();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);
        item.setEnabled(1);

        super.save(item);
    }

    /**
     * 更新故障处理措施
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamFaultHandleMeasureDTO dto) {

        // 保存
        EamFaultHandleMeasure item = new EamFaultHandleMeasure();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除故障处理措施(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamFaultHandleMeasureDTO dto) {

        EamFaultHandleMeasure item = new EamFaultHandleMeasure();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamFaultHandleMeasure> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamFaultHandleMeasure item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取故障处理措施信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamFaultHandleMeasureDTO get(String id) {
        // 查询数据
        EamFaultHandleMeasure item = this.baseMapper.selectById(id);
        EamFaultHandleMeasureDTO dto = new EamFaultHandleMeasureDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 插入故障处理措施
     *
     * @param list 批量插入故障处理措施
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<EamFaultHandleMeasureUploadExcel> list) {

        List<EamFaultHandleMeasure> savelist = new ArrayList<>();
        list.forEach(itemOne -> {
            // 保存
            EamFaultHandleMeasure item = new EamFaultHandleMeasure();
            BeanUtils.copyProperties(itemOne, item);
            this.setDefaultValue(item);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBigBatch(savelist);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamFaultHandleMeasureDTO>
     */
    public List<EamFaultHandleMeasureDTO> getList(EamFaultHandleMeasureQuery query) {
        // 查询条件
        QueryWrapper<EamFaultHandleMeasure> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind()!=null, EamFaultHandleMeasure::getAssetKind, query.getAssetKind());
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetTypeId()), EamFaultHandleMeasure::getAssetTypeId, query.getAssetTypeId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getFaultModeId()), EamFaultHandleMeasure::getFaultModeId, query.getFaultModeId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getPhenomenonId()), EamFaultHandleMeasure::getPhenomenonId, query.getPhenomenonId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getFaultReasonId()), EamFaultHandleMeasure::getFaultReasonId, query.getFaultReasonId());
        qw.lambda().like(!StrUtil.isEmpty(query.getFaultHandleMeasure()), EamFaultHandleMeasure::getFaultHandleMeasure, query.getFaultHandleMeasure());
        qw.lambda().eq(EamFaultHandleMeasure::getDeleted, DeleteFlag.NOTDELETED.getValue());
        qw.lambda().eq(EamFaultHandleMeasure::getEnabled, AvailableStatus.ENABLE.getValue());
        // 查询数据
        List<EamFaultHandleMeasure> list = super.list(qw);

        return convertList(list, EamFaultHandleMeasureDTO.class);
    }

    /**
     * 根据主键获取故障处理措施信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamFaultHandleMeasure item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

